package com.p2p.backend.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.p2p.backend.dao.FrontUserDao;
import com.p2p.backend.service.BackIdcardService;
import com.p2p.front.dal.dao.BorrowerPhotoDao;
import com.p2p.front.dal.model.IdCardInfo;
import com.p2p.front.dal.model.IdCardInfo.IdCardStatus;
import com.p2p.front.enums.PhotoType;
import com.p2p.front.service.IdCardService;

/**
 * leiwen@yiqihao.com
 */
@Service
public class BackIdCardServiceImpl implements BackIdcardService {
	@Resource
	private IdCardService idCardService;

	@Resource
	private FrontUserDao frontUserDao;

	@Resource
	private BorrowerPhotoDao borrowerPhotoDao;
	
	@Override
	@Transactional
	public void authIdCard(IdCardInfo idCard) {
		idCardService.updateIdCardStatus(idCard);
		//认证审核通过，才同步信息到user表
		if(idCard.getStatus()==IdCardStatus.SUCCEED.getStatus()){
			IdCardInfo idCardInfo=idCardService.getAuthing(idCard.getUid());
			String realname=idCardInfo.getRealName();
			String idcard=idCardInfo.getIdCard();
			String uid=idCardInfo.getUid();
			frontUserDao.authIdCard(uid, realname, idcard);
		}else if(idCard.getStatus()==IdCardStatus.FAILED.getStatus()){
			borrowerPhotoDao.removeByUidAndType(idCard.getUid(), PhotoType.IDCARD.getCode());
		}
	}
}
